% Author: Yang Ge, geyang_xjnu@sina.com
% 
% generate the sparks of the i'th firework
function result = generate_new_sparks(ith_seed, sparks_number, amplitude)
global dimension;
global upper_bound;
global lower_bound;

ith_sons_sparks_matrix = zeros(sparks_number, dimension);

% randomly select z dimensions 
z = randi([1,dimension], 1);
random_dimension_collection = randi([1, dimension], 1, z);

% randomly generate the new sprks of the ith fire work
for i = 1:sparks_number
    ith_new = ith_seed;
    % calculate the displacement in range rand(-1,1) * amplitude
    displacement = (rand * 2 - 1) * amplitude;
    for j = 1:length(random_dimension_collection)
        ith_new(random_dimension_collection(j)) = ...
                        ith_new(random_dimension_collection(j)) + displacement;
        
        % boundary checking
        % map new spraks to the potential space
        if (ith_new(random_dimension_collection(j)) < lower_bound) || ...
                (ith_new(random_dimension_collection(j)) > upper_bound)
            ith_new(random_dimension_collection(j)) = lower_bound + ...
                rem(ith_new(random_dimension_collection(j)), (upper_bound - lower_bound)); 
        end
    end
    ith_sons_sparks_matrix(i,:) = ith_new;
end
result = ith_sons_sparks_matrix;
end